package com.air.designmodel.eight;

/**
 * 单例模式
 *
 * @author shent
 */
public class Singleton {
    /**
     * volatile 确保当 uniqueInstance 变量被初始化为 Singleton 实例时.多个线程正确处理 uniqueInstance 变量
     */
    private volatile static Singleton uniqueInstance;

    private Singleton() {
    }

    public static Singleton getInstance() {
        //延迟实例化 + 双重检查加锁 处理性能和多线程问题
        if (uniqueInstance == null) {
            synchronized (Singleton.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new Singleton();
                }
            }
        }
        return uniqueInstance;
    }
}
